<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                 |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\CarDealer;

use AuctionCore\Account\Back\Employee;
use AuctionCore\Other\Address;
use AuctionCore\AntNest\UserCenter;

/**
 * 车商签约
 *
 * @author Xmh
 */
class DealerPersonalCertify
{
    private $CarDealer;
    private $CarDealerCertifyTraceLog;
    private $CarDealerSignLog;
    private $Addr;

    public function __construct()
    {
        $this->CarDealer = M('car_dealer');
        $this->CarDealerCertifyTraceLog = M('car_dealer_certify_trace_log');
        $this->CarDealerSignLog = M('car_dealer_sign_log');
        $this->Addr = new Address();
    }

    /**
     * 获取车商个人认证签约跟踪列表
     * @param $page
     * @param $limit
     * @param $params
     * @return array
     */
    public function getPersonalCertifyList($page, $limit, $params)
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $limit = (int)$limit < 1 ? 10 : (int)$limit;
        //搜索
        $map = array();
        $map['cd.auth_type'] = ['eq', 1];
        $map['cd.auth_status'] = ['eq', 2];
        if ($params['status'] == 1) { //待签约
            $map['cd.is_agree_agreement'] = ['eq', 0];
            $map['cd.sign_trace_status'] = ['eq', 0];
        } elseif ($params['status'] == 2) {  //签约跟踪
            $map['cd.is_agree_agreement'] = ['eq', 0];
            $map['cd.sign_trace_status'] = ['eq', 1];
            $map['cdsl.sign_result'] = ['eq', 1];
        } elseif ($params['status'] == 3) {  //跟踪签约成功
            $map['cd.is_agree_agreement'] = ['eq', 1];
            $map['cd.sign_trace_status'] = ['eq', 3];
            $map['cdsl.sign_result'] = ['eq', 3];
        } elseif ($params['status'] == 4) {  //跟踪签约失败
            $map['cd.is_agree_agreement'] = ['eq', 0];
            $map['cd.sign_trace_status'] = ['eq', 2];
            $map['cdsl.sign_result'] = ['eq', 2];
        }
        if ($params['dealer_id'] > 0) {
            $map['cd.dealer_id'] = intval($params['dealer_id']);
        }
        if ($params['dealer_name']) {
            $map['u.real_name'] = ['LIKE', '%' . $params['dealer_name'] . '%'];
        }
        if ($params['dealer_idcard']) {
            $map['cd.dealer_idcard'] = ['LIKE', '%' . $params['dealer_idcard'] . '%'];
        }
        if ($params['phone']) {
            $uc = new UserCenter();
            $info1 = $uc->info_bymobile($params['phone'], 2, 1);
            $map['du.uid'] = ['eq', $info1['account_id']];
            //$map['cd.contact_mobile'] = ['LIKE', '%' . $params['phone'] . '%'];
        }
        if ($params['start_time']) {
            if ($params['status'] == 1) {
                $map['cd.auth_audit_time'] = array('egt', $params['start_time'] . ' 00:00:00');
            } elseif ($params['status'] == 2) {
                $map['cdsl.next_trace_time'] = array('egt', $params['start_time'] . ' 00:00:00');
            } elseif ($params['status'] == 3) {
                $map['cd.sign_time'] = array('egt', $params['start_time'] . ' 00:00:00');
            } elseif ($params['status'] == 4) {
                $map['cdsl.updatetime'] = array('egt', $params['start_time'] . ' 00:00:00');
            }
        }
        if ($params['end_time']) {
            if ($params['status'] == 1) {
                $map['cd.auth_audit_time'] = array('egt', $params['end_time'] . ' 23:59:59');
            } elseif ($params['status'] == 2) {
                $map['cdsl.next_trace_time'] = array('egt', $params['end_time'] . ' 23:59:59');
            } elseif ($params['status'] == 3) {
                $map['cd.sign_time'] = array('egt', $params['end_time'] . ' 23:59:59');
            } elseif ($params['status'] == 4) {
                $map['cdsl.updatetime'] = array('egt', $params['end_time'] . ' 23:59:59');
            }
        }
        if ($params['start_time'] && $params['end_time']) {
            if ($params['status'] == 1) {
                $map['cd.auth_audit_time'] = array(array('egt', $params['start_time'] . ' 00:00:00'), array('elt', $params['end_time'] . ' 23:59:59'));
            } elseif ($params['status'] == 2) {
                $map['cdsl.next_trace_time'] = array(array('egt', $params['start_time'] . ' 00:00:00'), array('elt', $params['end_time'] . ' 23:59:59'));
            } elseif ($params['status'] == 3) {
                $map['cd.sign_time'] = array(array('egt', $params['start_time'] . ' 00:00:00'), array('elt', $params['end_time'] . ' 23:59:59'));
            } elseif ($params['status'] == 4) {
                $map['cdsl.updatetime'] = array(array('egt', $params['start_time'] . ' 00:00:00'), array('elt', $params['end_time'] . ' 23:59:59'));
            }
        }
        $cols = 'cd.dealer_id,cd.contact_person,cd.dealer_idcard,cd.business_city,cd.auth_type,
                 cd.apply_auth_time,cd.auth_audit_time,cd.sign_time,cd.createtime as reg_date,du.uid,
                 cdsl.next_trace_time,cdsl.sign_failed_remark,cdsl.updatetime as sign_failed_time';
        $prefix = C('DB_PREFIX');
        if ($params['status'] == 1) {
            $order = 'cd.auth_audit_time DESC';
        } elseif ($params['status'] == 2) {
            $order = 'cdsl.next_trace_time DESC';
        } elseif ($params['status'] == 3) {
            $order = 'cd.sign_time DESC';
        } elseif ($params['status'] == 4) {
            $order = 'cdsl.updatetime DESC';
        } else {
            $order = 'cd.dealer_id DESC';
        }

        $total = $this->CarDealer->alias('cd')
            ->join($prefix . "dealer_user as du ON du.dealer_id = cd.dealer_id")
            ->join(" LEFT JOIN " . $prefix . "car_dealer_sign_log as cdsl ON cdsl.dealer_id = cd.dealer_id AND sign_type = 1")
            ->where($map)->count();
        $list = $this->CarDealer->alias('cd')->field($cols)
            ->join($prefix . "dealer_user as du ON du.dealer_id = cd.dealer_id")
            ->join(" LEFT JOIN " . $prefix . "car_dealer_sign_log as cdsl ON cdsl.dealer_id = cd.dealer_id AND sign_type = 1")
            ->where($map)
            ->limit($limit)
            ->page($page)
            ->order($order)
            ->select();
        if (!empty($list)) {
            $dealer = new Dealer();
            $list = $dealer->get_contact_mobile_byAntNest($list);
        }
//        echo M()->_sql();
        if (!empty($list)) {
            foreach ($list as $key => $val) {
                $list[$key]['business_city_name'] = $val['business_city'] ? $this->Addr->get_name_by_code($val['business_city']) : '未指定';
                $list[$key]['auth_type'] = C('DEALER_CERTIFY_TYPE')[$val['auth_type']];
                $list[$key]['next_trace_time'] = $val['next_trace_time'] ? $val['next_trace_time'] : '--';
                $list[$key]['dealer_name'] = $val['contact_person'];
            }
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 获取车商个人签约详情
     *
     * @param $dealer_id
     * @return bool|mixed
     */
    public function getPersonalCertifyDetail($dealer_id)
    {
        if ($dealer_id < 1) {
            return false;
        }
        $condition['cd.dealer_id'] = ['eq', $dealer_id];
        //要查询的列表
        $cols = 'cd.dealer_id,cd.dealer_idcard,cd.contact_person,cd.sign_time,cd.auth_type,cd.auth_status,
                 cd.is_agree_agreement,cd.idcard_front_pic,cd.idcard_back_pic,cd.idcard_hold_pic,cd.sign_contract_no,
                 cd.createtime as reg_date,du.uid';
        $prefix = C('DB_PREFIX');
        $res = $this->CarDealer->alias('cd')->field($cols)
            ->join($prefix . "dealer_user as du ON du.dealer_id = cd.dealer_id")
            ->where($condition)->find();
        $res['idcard_front_pic'] = get_content_cdnurl().$res['idcard_front_pic'];
        $res['idcard_back_pic'] = get_content_cdnurl().$res['idcard_back_pic'];
        $res['idcard_hold_pic'] = get_content_cdnurl().$res['idcard_hold_pic'];
        if (!empty($res)) {
            $dealer = new Dealer();
            $res = $dealer->get_contact_mobile_byAntNestfind($res);
        }
        $res['dealer_name'] = $res['contact_person'];
        return $res;
    }

}
